package com.scuec.tourismwebsite.service;

import com.scuec.tourismwebsite.entity.TouristAttraction;
import com.scuec.tourismwebsite.mapper.TouristAttractionMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;
//1
@Service
public class TouristAttractionServiceImpl implements TouristAttractionService {
    @Autowired
    private TouristAttractionMapper touristAttractionMapper;

    @Override
    public List<TouristAttraction> findAll() {
        return touristAttractionMapper.findAll();
    }

    @Override
    public ResponseEntity<TouristAttraction> findById(Integer attraction_id) {
        TouristAttraction attraction = touristAttractionMapper.findById(attraction_id);
        if (attraction != null) {
            return ResponseEntity.ok(attraction);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @Override
    public ResponseEntity<TouristAttraction> add(TouristAttraction attraction) {
        try {
            Integer city_id = touristAttractionMapper.findCityIdByLocation(attraction.getLocation());
            if (city_id == null) {
                return ResponseEntity.badRequest().body(null);
            }
            attraction.setCity_id(city_id);
            int result = touristAttractionMapper.insert(attraction);
            if (result > 0) {
                return ResponseEntity.ok(attraction);
            } else {
                return ResponseEntity.status(500).body(null);
            }
        } catch (Exception e) {
            e.printStackTrace();  // 打印详细的错误信息
            return ResponseEntity.status(500).body(null);
        }
    }




    @Override
    public ResponseEntity<TouristAttraction> updateBasicInfo(Integer attraction_id, TouristAttraction attraction) {
        attraction.setAttraction_id(attraction_id);
        int result = touristAttractionMapper.updateBasicInfo(attraction);
        if (result > 0) {
            return ResponseEntity.ok(attraction);
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    @Override
    public ResponseEntity<TouristAttraction> updateDetail(Integer attraction_id, TouristAttraction attraction) {
        //attraction.setAttraction_id(attraction_id);
        int result = touristAttractionMapper.updateDetail(attraction);
        if (result > 0) {
            return ResponseEntity.ok(attraction);
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    @Override
    public ResponseEntity<Void> delete(Integer attraction_id) {
        int result = touristAttractionMapper.delete(attraction_id);
        if (result > 0) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    @Override
    public PageInfo<TouristAttraction> findAttractionsPages(Integer pageNum, Integer pageSize) {
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<TouristAttraction> lists = touristAttractionMapper.findAll();
            PageInfo<TouristAttraction> pageInfo = new PageInfo<>(lists);
            return pageInfo;
        } catch (Exception e) {
            return new PageInfo<>(null);
        }
    }
    @Override
    public PageInfo<TouristAttraction> findAttractionsPage(String location, Integer pageNum, Integer pageSize) {
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<TouristAttraction> lists = touristAttractionMapper.findByLocation(location);
            PageInfo<TouristAttraction> pageInfo = new PageInfo<>(lists);
            return pageInfo;
        } catch (Exception e) {
            return new PageInfo<>(null);
        }
    }

    @Override
    public PageInfo<TouristAttraction> searchAttractions(String query, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<TouristAttraction> lists = touristAttractionMapper.searchByQuery(query);
        PageInfo<TouristAttraction> pageInfo = new PageInfo<>(lists);
        return pageInfo;
    }

    @Override
    public TouristAttraction getAttractionDetailById(Integer attraction_id) {
        return touristAttractionMapper.findDetailById(attraction_id);
    }

    @Override
    public boolean updatePhoto(Integer attraction_id, String photos) {
        TouristAttraction attraction = new TouristAttraction();
        attraction.setAttraction_id(attraction_id);
        attraction.setPhotos(photos);
        int affectedRows = touristAttractionMapper.updatePhoto(attraction);
        return affectedRows > 0;
    }

    // TouristAttractionServiceImpl.java
    // TouristAttractionServiceImpl.java
    @Override
    public ResponseEntity<TouristAttraction> update(Integer attractionId, TouristAttraction attraction) {
        attraction.setAttraction_id(attractionId);
        int result = touristAttractionMapper.updateDetail(attraction);
        if (result > 0) {
            TouristAttraction updatedAttraction = touristAttractionMapper.findById(attractionId);
            return ResponseEntity.ok(updatedAttraction);
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }
}